﻿using LuShanTea.DTO;
using PlainElastic.Net;
using PlainElastic.Net.Queries;
using PlainElastic.Net.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Web.Common
{
    public class LuShanTeaElasticSearch
    {
        public static bool Insert(GoodDTO p1)
        {

            ElasticConnection client = new ElasticConnection("127.0.0.1", 9200); // 具体上线配置成服务器ip地址就行
            var serializer = new JsonNetSerializer();
            //第一个参数相当于“数据库”，第二个参数相当于“表”，第三个参数相当于“主键”
            IndexCommand cmd = new IndexCommand("LuShanTea", "Goods", p1.Id.ToString());
            //Put()第二个参数是要插入的数据
            OperationResult result = client.Put(cmd, serializer.Serialize(p1));

            string res = result.Result;
            string[] ress = res.Split(',');
            Console.WriteLine(ress.Count());
            Console.WriteLine(ress[4]);
            string[] ress2 = ress[4].Split(':');
            Console.WriteLine(ress2[1]);
            string s = ress2[1].Trim('"');
            if (s.Equals("updated") || s.Equals("created"))
            {
                return true;

            }
            return false;
        }

        public static GoodDTO[] Search(QueryBuilder<GoodDTO> q)
        {
            ElasticConnection client = new ElasticConnection("127.0.0.1", 9200);
            SearchCommand cmd = new SearchCommand("LuShanTea", "Goods");
            //var query = new QueryBuilder<GoodDTO>()
            //.Query(b =>
            // b.Bool(m =>
            ////并且关系
            //m.Must(t =>
            // //分词的最小单位或关系查询
            // t.QueryString(t1 => t1.DefaultField("Descible").Query("好吃"))
            // )
            // )
            //)
            //.Build();
            var query =q.Build();
            var result = client.Post(cmd, query);
            var serializer = new JsonNetSerializer();
            var list = serializer.ToSearchResult<GoodDTO>(result);
            Console.WriteLine("查询结果总数："+list.Documents.Count());
            List<GoodDTO> listDtos = new List<GoodDTO>();

            foreach (var item in list.Documents)
            {
                GoodDTO dto = new GoodDTO();
                dto.CateGoriesId = item.CateGoriesId;
                dto.CityId = item.CityId;
                dto.CreateDateTime = item.CreateDateTime;
                dto.Descible = item.Descible;
                dto.Id = item.Id;
                dto.Name = item.Name;
                dto.PackIds = item.PackIds;
                dto.PrePrice = item.PrePrice;
                dto.Price = item.Price;
                dto.Selled = item.Selled;
                dto.TotalCount = item.TotalCount;
                dto.Weight = item.Weight;
                listDtos.Add(dto);
                //Console.WriteLine(item.GetType());//LuShanTea.DTO.GoodDTO
                //Console.WriteLine($"ID:{item.Id}-商品名称-{item.Name}-商品价格-{item.Price}-{item.CityName}--商品描述--{item.Descible}");
                //Console.WriteLine(item.Id + "-" + item.CatoryName);
            }
            return listDtos.ToArray();
        }
    }
}
